package com.lczyfz.istep.modules.openstack.api.nova.impl;

import com.lczyfz.istep.modules.openstack.api.neutron.model.Network;
import com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson.NovaParse;
import com.lczyfz.istep.modules.openstack.api.nova.interfaces.VirtualMachine;
import com.lczyfz.istep.modules.openstack.api.nova.model.Server;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

/**
 * VirtualMachine�ӿڵ�ʵ����
 * @author jm
 */
public class VirtualMachineImpl implements VirtualMachine {
	
	public RequestFactory factory = new RequestFactory("nova");
	
	/**
	 * �оٳ����е������
	 * @return
	 */
	public List<Server> getServers(){
		
		List<Server> servers = null;
		Request request = factory.getRequest();
		request.setUrl("/servers");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);

		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		servers = NovaParse.resolveServers(body);
		return servers;
	}
	
	/**
	 * �оٳ����е�����
	 * @return
	 */
	public List<Network> getNetworks(){
		List<Network> nets = null;
		
		Request request = factory.getRequest();
		request.setUrl("/os-networks");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);

		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		nets = NovaParse.resolveNets(body);
		return nets;
	}
	
	/**
	 * ����Id�г������
	 */
	@Override
	public Server getServerById(String serverId,String ctfNetworkName) {
		Server server = null;
		try{

			Request request = factory.getRequest();
			request.setUrl("/servers/" + serverId);
			request.setRequestType(RequestType.GET);
			Response results = HTTP.getConnectionResult(request);

//			if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
//				throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
//			}
			JSONObject body = results.getResponseBody();
			server = NovaParse.resolveServerDetail(body,ctfNetworkName);
		}catch (Exception e)
		{
			System.out.println("获取IP失败");
			e.printStackTrace();
		}

		return server;
	}
	
	@Override
	public Server addServer(Server server) {

		Server servers = null;
		JSONObject params = NovaParse.packageServer(server);

		Request request = factory.getRequest();

		request.setUrl("/servers");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		System.out.println("2=============================" + request.toString());
		Response results = HTTP.getConnectionResult(request);
		System.out.println("=============================");
		if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){

			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		servers = NovaParse.resolveCreateServer(body);
		return servers;
	}
	
	@Override
	public Server updateServer(String serverId,Server server) {
		Server servers = null;
		JSONObject params = NovaParse.packageServer(server);
		
		Request request = factory.getRequest();
		request.setUrl("/servers/" + serverId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		servers = NovaParse.resolveUpdateServer(body);
		return servers;
	}
	
	@Override
	public boolean deleteServer(String serverId) {
		Request request = factory.getRequest();
		request.setUrl("/servers/" + serverId);
		request.setRequestType(RequestType.DELETE);
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return true;
	}

	public static void main(String[] args){

		VirtualMachineImpl nova = new VirtualMachineImpl();
		nova.getServers();
		nova.getNetworks();
		List<Server> servers = nova.getServers();
		System.out.println("虚拟机列表");
		for(Server server : servers){
			System.out.println(server.getName() + " " + server.getId()+" "+server.getStatus());
		}
		Server server=nova.getServerById("d70d0513-10cb-49da-906b-a8665c5bf667", "s1");
		System.out.println("server="+server.getId()+" "+server.getName()+" "+server.getIp());

//		Server newServer = new Server();
//		newServer.setName("vm2");
//		newServer.setFlavorRef("3a5d1c85-62ec-423b-bda8-e80b33232826");
//		newServer.setImageRef("1acb90ee-1582-4617-b4da-50f62d9d01a9");
//		JSONArray ports = new JSONArray();

//		PortImpl portImpl=new PortImpl();
//		Port port =new Port();
//		port.setPortNetworkId("34823510-5ee6-4c22-86f8-1f9b8ec480a5");
//		JSONArray fixedIps = new JSONArray();
//		JSONObject fixedIp = new JSONObject();
//		fixedIp.put("ip_address","192.168.0.15");
//		fixedIps.add(0, fixedIp);
//		port.setPortFixedIp(fixedIps);
//		Port newPort =portImpl.createPort(port);

//		System.out.println("port="+newPort.getPortId());

//		JSONObject port1 = new JSONObject();
//		port1.put("port", "b04af9c5-b8af-4140-874d-8524c9872a57");
//		ports.add(0, port1);
//		newServer.setPorts(ports);
//		JSONArray networks = new JSONArray();
//		JSONObject network1 = new JSONObject();
//		JSONObject network2 = new JSONObject();
//		network1.put("uuid", "39f895f8-fa92-4f9d-9be9-1573e69bc70c");
//		networks.add(0, network1);
//		newServer.setNetworks(networks);
//		Server reServer = nova.addServer(newServer);
//		System.out.println("res="+reServer.getId());
//		System.out.println("���������");
//		Server newServer = new Server();
//		newServer.setName("1234567");
//		newServer.setFlavorRef("1");
//		newServer.setImageRef("94df9233-0f79-4f8c-b385-706be94fbd08");
//


		
//		System.out.println("���������");
//		serverById.setName("7654321");
//		Server res2 = nova.updateServer(res.getId(), serverById);
//
//		System.out.println("ɾ�������");
//		nova.deleteServer(res2.getId());
	}

}
